package com.example.peibei.ui.activity;

import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.dingtao.common.bean.User;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.util.SPUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.ui.fragment.MyGetOrderListFragment;
import com.example.peibei.ui.fragment.MyWaitOrderListFragment;
import com.example.peibei.ui.fragment.PaidanListFragment;
import com.example.peibei.ui.weight.MySlidingTabLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderPlayingActivity extends WDActivity {
    @BindView(R.id.slide_tablayout)
    MySlidingTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tv_title)
    TextView tv_title;

    private String[] title={"下单","接单"};
    private ArrayList<Fragment> fragments=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_wait;
    }

    @Override
    protected void initView() {
        tv_title.setText("进行中");
        SPUtils spUtils = new SPUtils(this, SpConstant.USER_TOKEN);

        List<String> titleList = new ArrayList<>();

        User loginUser = SpConstant.getLoginUser();
        boolean buyItOrder = loginUser.isShowBuyItOrder();
        if (buyItOrder){
            titleList.add("下单");
            fragments.add(new MyGetOrderListFragment(1));
        }

        boolean identity = loginUser.isShowServiceOrder();
        if (identity){
            fragments.add(new MyWaitOrderListFragment(1));
            titleList.add("接单");
        }

        boolean showDispathOrder = loginUser.isShowDispatchOrder();
        if (showDispathOrder){
            PaidanListFragment fragment = new PaidanListFragment().setType("1");
            fragments.add(fragment);
            titleList.add("派单");
        }

        if (fragments.isEmpty()){
            findViewById(R.id.empty_view).setVisibility(View.VISIBLE);
            return;
        }

        String[] title= titleList.toArray(new String[titleList.size()]);
        viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title[position];
            }
        });
        tabLayout.setViewPager(viewpager,title,this,fragments);
        Drawable drawable = getDrawable(R.drawable.underline_color);
        tabLayout.setDrawable(drawable);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                for (int i = 0; i < title.length; i++) {
                    TextView textView = tabLayout.getTitleView(i);
                    if (i == position){
                        //设置为加粗
                        textView .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        textView.setTextSize(18);
                    }else {
                        //设置为不加粗
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        textView.setTextSize(14);
                    }
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < title.length; i++) {
                    TextView textView = tabLayout.getTitleView(i);
                    if (i == position){
                        //设置为加粗
                        textView .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                        textView.setTextSize(18);
                    }else {
                        //设置为不加粗
                        textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                        textView.setTextSize(14);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        TextView textView = tabLayout.getTitleView(0);
        //设置为加粗
        textView .setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        textView.setTextSize(18);
    }

    @OnClick({R.id.iv_back,R.id.tv_title})
    public void back(){
        finish();
    }

    @Override
    protected void destoryData() {

    }
}
